package com.ruoyi.web.service.Impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.pinyin.PinYinUtils;
import com.ruoyi.web.domain.mes.*;
import com.ruoyi.web.domain.vo.MaterialInVo;
import com.ruoyi.web.domain.vo.MaterialInventoryVo;
import com.ruoyi.web.mapper.MesMaterialInMapper;
import com.ruoyi.web.mapper.MesMaterialMapper;
import com.ruoyi.web.mapper.MesConfigMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.web.mapper.MesMaterialInventoryMapper;
import com.ruoyi.web.service.IMesMaterialInventoryService;

import javax.annotation.Resource;

/**
 * 物料盘点Service业务层处理
 *
 * @author levicash
 * @date 2024-01-31
 */
@Service
public class MesMaterialInventoryServiceImpl implements IMesMaterialInventoryService {
    @Resource
    private MesMaterialInventoryMapper mesMaterialInventoryMapper;

    @Resource
    private MesMaterialInMapper mesMaterialInMapper;

    @Resource
    private MesMaterialMapper mesMaterialMapper;
    @Resource
    private MesConfigMapper configMapper;

    /**
     * 查询物料盘点
     *
     * @param id 物料盘点主键
     * @return 物料盘点
     */
    @Override
    public MesMaterialInventory selectMesMaterialInventoryById(Long id) {
        return mesMaterialInventoryMapper.selectMesMaterialInventoryById(id);
    }

    /**
     * 查询物料盘点列表
     *
     * @param mesMaterialInventory 物料盘点
     * @return 物料盘点
     */
    @Override
    public List<MesMaterialInventory> selectMesMaterialInventoryList(MesMaterialInventory mesMaterialInventory) {
        return mesMaterialInventoryMapper.selectMesMaterialInventoryList(mesMaterialInventory);
    }

    /**
     * 新增物料盘点
     *
     * @param mesMaterialInventory 物料盘点
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMesMaterialInventory(MesMaterialInventory mesMaterialInventory) {
        mesMaterialInventory.setCreateTime(DateUtils.getNowDate());
        mesMaterialInventory.setCreateBy(SecurityUtils.getUsername());
        // 生成盘点单编号
        Map<String, Object> map = new HashMap<>();
        map.put("columnName", "code");
        map.put("tableName", "mes_material_inventory");
        int maxNum = configMapper.getMaxId(map);
        String code = PinYinUtils.getCodeNormal(maxNum, "WLPDD");
        mesMaterialInventory.setCode(code);
        int rows = mesMaterialInventoryMapper.insertMesMaterialInventory(mesMaterialInventory);
        insertMesMaterialInventorydetail(mesMaterialInventory);
        return rows;
    }

    /**
     * 修改物料盘点
     *
     * @param mesMaterialInventory 物料盘点
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMesMaterialInventory(MesMaterialInventory mesMaterialInventory) {
        mesMaterialInventory.setUpdateTime(DateUtils.getNowDate());
        mesMaterialInventory.setUpdateBy(SecurityUtils.getUsername());
        int status = mesMaterialInventory.getStatus();
        if (status == 1) {// 提交
            // 修改物料入库明细的仓库数量(覆盖库存)
            MesMaterialInventory Info = mesMaterialInventoryMapper.selectMesMaterialInventoryById(mesMaterialInventory.getId()); // 查询数据库
            List<MesMaterialInventorydetail> mesMaterialInventorydetailList = Info.getMesMaterialInventorydetailList();
            if (mesMaterialInventorydetailList.size() > 0) {
                // 获取关联ids
                List<Long> listId = mesMaterialInventorydetailList.stream().map(it -> it.getMaterialId()).collect(Collectors.toList());
                Long[] ids = listId.toArray(new Long[0]);
                // 根据关联ids查询物料明细
                List<MesMaterial> detailList = mesMaterialMapper.selectMesMaterialByIds(ids);
                // 遍历明细修改库存
                for (MesMaterialInventorydetail mesMaterialInventorydetail : mesMaterialInventorydetailList) {
                    for (MesMaterial detailItem : detailList) {
                        if (detailItem.getId().equals(mesMaterialInventorydetail.getMaterialindetailId())) {
                            BigDecimal storeQuantity = mesMaterialInventorydetail.getStoreQuantity();// 库存数量
                            BigDecimal inventoryQuantity = mesMaterialInventorydetail.getInventoryQuantity(); // 手输的盘点数量
                            // 保存本次库存
                            mesMaterialInventorydetail.setIndetailQuantity(storeQuantity);
                            detailItem.setQuantity(inventoryQuantity);         // 更改库存数量
                            detailItem.setStockPrice(inventoryQuantity.multiply(detailItem.getRealtimePrice()));  // 更改金额
                        }
                    }
                }
                // 批量修改物料明细库存
                mesMaterialMapper.updateStockNumByBatch(detailList);
            }
        } else {
            mesMaterialInventoryMapper.deleteMesMaterialInventorydetailByMaterialinventoryId(mesMaterialInventory.getId());
            insertMesMaterialInventorydetail(mesMaterialInventory);
        }
        return mesMaterialInventoryMapper.updateMesMaterialInventory(mesMaterialInventory);
    }

    /**
     * 批量删除物料盘点
     *
     * @param ids 需要删除的物料盘点主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesMaterialInventoryByIds(Long[] ids) {
        mesMaterialInventoryMapper.deleteMesMaterialInventorydetailByMaterialinventoryIds(ids);
        return mesMaterialInventoryMapper.deleteMesMaterialInventoryByIds(ids);
    }

    /**
     * 删除物料盘点信息
     *
     * @param id 物料盘点主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesMaterialInventoryById(Long id) {
        mesMaterialInventoryMapper.deleteMesMaterialInventorydetailByMaterialinventoryId(id);
        return mesMaterialInventoryMapper.deleteMesMaterialInventoryById(id);
    }

    /**
     * 新增物料盘点明细信息
     *
     * @param mesMaterialInventory 物料盘点对象
     */
    public void insertMesMaterialInventorydetail(MesMaterialInventory mesMaterialInventory) {
        List<MesMaterialInventorydetail> mesMaterialInventorydetailList = mesMaterialInventory.getMesMaterialInventorydetailList();
        Long id = mesMaterialInventory.getId();
        if (StringUtils.isNotNull(mesMaterialInventorydetailList)) {
            List<MesMaterialInventorydetail> list = new ArrayList<MesMaterialInventorydetail>();
            for (MesMaterialInventorydetail mesMaterialInventorydetail : mesMaterialInventorydetailList) {
                mesMaterialInventorydetail.setCreateBy(SecurityUtils.getUsername());
                mesMaterialInventorydetail.setCreateTime(DateUtils.getNowDate());
                mesMaterialInventorydetail.setMaterialinventoryId(id);
                list.add(mesMaterialInventorydetail);
            }
            if (list.size() > 0) {
                mesMaterialInventoryMapper.batchMesMaterialInventorydetail(list);
            }
        }
    }

    /**
     * 查询物料盘点明细
     *
     * @param map 查询参数
     * @return 物料盘点关联集合
     */
    @Override
    public List<MaterialInventoryVo> selectMesMaterialInventoryDetailList(Map<String, Object> map) {
        return mesMaterialInventoryMapper.selectMesMaterialInventoryDetailList(map);
    }
}
